package ExcelHelper

import (
	"fmt"
	"github.com/xuri/excelize/v2"
)

//excel个列的简称
//https://blog.csdn.net/wtt234/article/details/124531074
//
var CellCols = []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}

type writeExcel struct {
	title []string
	data  [][]string
	sheet string
}

func NewWriteExcel() *writeExcel {
	return &writeExcel{}
}

func (this *writeExcel) SetTitle(title []string) {
	this.title = title
}

func (this *writeExcel) SetData(data [][]string) {
	this.data = data
}
func (this *writeExcel) SetShellNum(sheelNum int) {
	this.sheet = fmt.Sprintf("Sheet%d", sheelNum)
}

func (this *writeExcel) Write(fileName string) error {
	f := excelize.NewFile()
	//titleLen := len(this.title)
	var resltErr error
	for k, v := range this.title {
		resltErr = f.SetCellValue(this.sheet, fmt.Sprintf("%s%d", CellCols[k], 1), v)
		if resltErr != nil {
			break
		}
	}
	if resltErr != nil {
		return resltErr
	}
	//写入数据
LABEL_STOP:
	for k, v := range this.data {
		for kv, vv := range v {
			resltErr = f.SetCellValue(this.sheet, fmt.Sprintf("%s%d", CellCols[kv], k+2), vv)
			if resltErr != nil {
				break LABEL_STOP
			}
		}
	}

	if resltErr != nil {
		return resltErr
	}

	//保存
	resltErr = f.SaveAs(fileName)
	if resltErr != nil {
		return resltErr
	}
	return nil
}
